import Vue from 'vue'
import VueRouter from 'vue-router'

import detail from '../views/detail.vue'
import login from '../views/login.vue'
import layout from '../views/layout.vue'
import register from '../views/register.vue'

import collect from '../views/collect.vue'
import article from '../views/article.vue'
import like from '../views/like.vue'
import user from '../views/user.vue'
import { getToken } from '@/utils/storage'
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  { path: '/detail', component: detail },
  { path: '/login', component: login },
  { path: '/register', component: register },
  {
    path: '/',
    component: layout,
    children: [
      { path: '/collect', component: collect },
      { path: '/like', component: like },
      { path: '/user', component: user },
      { path: '/article', component: article }
    ]
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    const whiteList = ['/login', '/register']
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      Toast.fail('亲,您还没有登录呢')
    }
  }
})

export default router
